Click here to Skip to main content
15,886,919 members
Articles / Database Development / SQL Server
Article

Handle Sql server and Foxpro database with .NET

Rate me:
Please Sign up or sign in to vote.
2.48/5 (12 votes)
6 May 2007CPOL6 min read 69.9K   1.8K   24   17
This article aids in rapid application development for database handling, for desktop and web applications

Introduction

This article hopes to provide the reader with rapid application development for database handling. It works not only with the desktop applications but web applications too. This dll is written in the C# language, but you can use it with any of the other language after compiling it.

I am not submitting the dll file because I know it needs a little more modification (connection string, etc.) before it can be used. It can read the connection string in the text file but is not good with web applications, so you may have to make some changes in its Cnn class.

This article also helps those who are using SQL Server or .dbf (FoxPro) as a database. One of its classes, CopySqlAsDbf, has the ability to convert tables from SQL to dbf. To use its FoxPro facility means to use its Dbf and SqlDbfClass classes; you need to download the Microsoft ODBC driver, which is free. (This article assumes that you have that driver)

This ddl is basically made for Windows-based projects, but I used it for web projects too, when I have lots of work to finish within a limited amount of time.

One of the main advantages of this dll, is that it speeds up your development process and it is very easy to understand. It basically has four classes.

  1. Cnn class
  2. Dbf class
  3. Sql class
  4. SqlDbfClass class

As the names suggest, all the classes are made for the different purposes.

Let's go one class at a time:

Cnn class

It has two methods, named:
C#
cnnStringValue //for sql server connection string.

    public string cnnStringValue()
    {
        try
        {
            TextReader tr = new StreamReader("connectionString.txt");
            cnnString=tr.ReadLine().ToString().Trim();
        }
        catch//(Exception ex)
        {
            //do nothing
            //throw new Exception(ex.Message);
        }
        return cnnString;
    }

Here I am reading the connectionstring from a text file, which is more convenient for software deployment because sometimes the client is using a proxy, or has different servers for their database, so we give them permission to set values from the text file. In the class Cnn, the variable cnnString has its own predefines values, so that if the file isn't found or any other error occurs then it returns the predefined value. This is because I do not handle the error by throwing an exception. You can do this if you need it, or think it is more convenient for you.

DBFcnnString //for dbf(foxpro)connection string. It simply returns dbfconnection string which is almost fixed. While creating, one thing you should have in your mind is the Application startup path. For example:

C#
Cnn conn=new Cnn(ApplicationStartupPath.Trim());
// will call the following methods:
    public Cnn(string mvarSysProjPath)
    {
        //string mvarSysProjPath =Application.StartupPath;
        DBFString="Provider=MSDASQL/SQLServer ODBC;Driver={
            Microsoft Visual FoxPro Driver};" + "SourceType=DBF;SourceDB=" + 
            mvarSysProjPath.Trim() + ";InternetTimeout=300000;
            Transact Updates=True";
    }

And set the DBFString variables with the connectionString value. Now you can receive that value simply:

C#
string strConnect=conn.DBFcnnString();

In my dll both methods are used for internal method calling. This means both are used by the other classes. You can use these functions according your requirements.

Dbf class

It has three methods, named:

  1. ExecuteDbfQuery
  2. ReturnDbfTableMaxid
  3. ReturnDbfTableValue

In spite of these methods this class has one parameterized constructor and one property, both for the same task. You can choose any one of them while using the class. Now we will look at these methods one by one:

ExecuteDbfQuery

  • ExecuteDbfQuery: We can use any methods of this class after setting its ApplicationPath property or by using parametrized constructor
C#
public Dbf(string path)
{
    ApplicationStartupPath=path;
} 

While making the object of the Dbf class, you should pass the application startup path as parameter.

C#
Dbf objDbf = new Dbf(Application.StartupPath);

public void ExecuteDbfQuery(string DbfQuery) method receives the DbfQuery as string. It can execute the Insert update and delete query in the FoxPro database. Just pass your insert query in DbfQuery like this:

C#
objDbf.ExecuteDbfQuery(
    "insert into tablename(column1,column2,……..) 
    values('"+value1+"','"+value2+"',……..)");

ReturnDbfTableMaxid

  • ReturnDbfTableMaxid: This is another useful function of this Class. It returns the maximum id or any auto increment field value. You should only pass the tablename and the columnName from which that value resides.
C#
public int ReturnDbfTableMaxid(string tableName , string columnName)

ReturnDbfTableValue

C#
public string ReturnDbfTableValue(string tableName, string columnName,
    string condition) 

This is also a very useful function of this class. It returns a single value of the column field based on the where condition. You should pass only the tablename, columnname (in which that value resides) and the where condition. There is a condition variable with the keyword where; this function generates the query and returns you that value.

Sql Class

It has three methods, named:

  1. ExecuteSqlQuery
  2. ReturnSqlTableMaxid
  3. ReturnSqlTableValue

Similar to the Dbf class, the Sql class also has the same methods to deal with the SQL database. You can also add more methods according to your requirements. A brief introduction of these methods is as follows: you can use any aspect of this function if you place the text file having the connection string in the same folder where this dll resides, generally in the bin folder or you can manually set the cnnString variable in the Cnn class constructor like this:

C#
public Cnn()
{ 
    cnnString="Server=servername/ipaddress;UID=username;
        PWD=password;Database=databasename";
}

And after set this parameter you can compile this dll and reference in your project. Methods are given below.

ExecuteSqlQuery

C#
public void ExecuteSqlQuery(string sqlQuery)

This class simply accepts the SQL insert, update, delete query and execute them, it also throws an exception if it occurs, you can print it if you want. The method calling looks like this:

C#
Sql objSql=new Sql();
objSql.ExecuteSqlQuery("update tablename set name='"+nameVariable+"'");

ReturnSqlTableMaxid

C#
public int ReturnSqlTableMaxid(string tableName , string columnName)

This class returns the SQL table max auto genrated id. It only accepts the tablename and the columnname of that auto generated id.

The method calling looks like this:

C#
Sql objSql=new Sql();
int maxid = objSql.ReturnSqlTableMaxid("tablename", "columnname");

ReturnSqlTableValue

C#
public string ReturnSqlTableValue(string tableName, string columnName, 
    string condition)

This class returns a single sqltable column value based on the conditions. This method accepts the tablename, columnName, the where condition and returns the required value. The method calling looks like this:

C#
Sql objSql=new Sql();
objSql.ReturnSqlTableValue("tablename", "columnname", 
    "where name='"+nameVariable+"'");

SqlDbfClass.cs file

Actually this has a SqlToDbfConvert class having only one method and one property, setApplicationPath. This property is to set the application startup path and the method CopySqlAsDbf to copy SQL table data into the dbf table, but it has some limitations.

C#
public void CopySqlAsDbf(string SqlQuery, string DbfTableName)

This method accepts the SQL select query and dbf table name (only the name) into which you want to copy the data. It automatically creates the table and dumps the data.

Here I use the SqlQuery variable instead of the SQL table name because you can use the where condition here too, so you can got the filtered data into the dbf table.

Steps to use this Dll

  1. Right click of the solution explorer, you will see the option add references. Choose "Add references" like this:

    Screenshot - image001.jpg

  2. It will now seems like this:

    Screenshot - image002.jpg

  3. Now you can use it as follows:
    C#
    using sqlDbfTransactions;

    if using C# you use the above code. You can use it in VB.NET in the same way using imports key word. Even if it is written in C#.

Conclusion

This dll helped me lot when I required fast development not only in the desktop but also in the web, and it is also very useful for those users who are working in the FoxPro database or use mix FoxPro and SQL Server as database.

Later I am adding more functions in this dll according to organizations requirement. You should do the same. I made many dynamic websites in a very short time using this dll. I hope you also enjoy it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer UTStarcom
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionView data? Pin
DanielEvansJr27-Dec-17 5:27
DanielEvansJr27-Dec-17 5:27 
Questionuse this dbf reader in windows mobile app Pin
mehdimirzaei29-Nov-11 8:19
mehdimirzaei29-Nov-11 8:19 
Generali need code to connect to DBF Pin
el3ashe26-Sep-09 23:44
el3ashe26-Sep-09 23:44 
GeneralRe: i need code to connect to DBF Pin
Gaurav K Singh7-Sep-09 0:03
Gaurav K Singh7-Sep-09 0:03 
Generaldbf connect lost Pin
winitpost7-Jul-08 17:58
winitpost7-Jul-08 17:58 
QuestionHow to use .DBF File in ASP.NET Pin
paramveerd14-May-08 22:02
paramveerd14-May-08 22:02 
AnswerRe: How to use .DBF File in ASP.NET Pin
stixoffire23-May-08 9:05
stixoffire23-May-08 9:05 
QuestionMicrosoft ODBC driver Pin
Lawler107914-Nov-07 9:00
Lawler107914-Nov-07 9:00 
QuestionODBC driver Pin
Valeri Makarov15-Aug-07 19:28
Valeri Makarov15-Aug-07 19:28 
GeneralAdditional idea Pin
schavlytko27-Jul-07 3:36
schavlytko27-Jul-07 3:36 
GeneralGreat help! Pin
Ron4407730-May-07 4:55
Ron4407730-May-07 4:55 
Generalwel done gaurav... Pin
Sarva Daman14-May-07 8:18
Sarva Daman14-May-07 8:18 
Generalgreat job Pin
ankur_sd14-May-07 8:17
ankur_sd14-May-07 8:17 
GeneralRe: great job Pin
cynsystems4-Sep-08 5:20
cynsystems4-Sep-08 5:20 
QuestionWhat? Pin
Elliott Keith10-May-07 21:16
Elliott Keith10-May-07 21:16 
AnswerRe: What? Pin
G. K. Singh10-May-07 21:36
G. K. Singh10-May-07 21:36 
GeneralRe: What? Pin
stixoffire23-May-08 9:03
stixoffire23-May-08 9:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.